package com.example.controller;


import com.example.common.Result;
import com.example.entity.Doctor;
import com.example.service.DoctorService;
import com.example.utils.RedisUtils;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 医生前端操作接口
 **/

/**
 * @Author: 张比特
 * @Data: 2024/8/30 上午8:51
 * @Version: v1.0.0
 * @Description: TODO
 **/
@RestController
@RequestMapping("/doctor")
public class DoctorController {

    @Resource
    private DoctorService doctorService;
    @Resource
    private RedisUtils redisUtils;
//    @Resource
//    RedisResult redisResult;


    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Doctor doctor) {
        doctorService.add(doctor);
        String key = doctor.getUsername();
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        doctorService.deleteById(id);
        // 删除redis中的数据
        String key = "doctor" + id;
        redisUtils.remove(key);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Doctor doctor) {
        doctorService.updateById(doctor);
        return Result.success();
    }

//    /**
//     * 根据ID查询
//     */
//    @GetMapping("/selectById/{id}")
//    public Result selectById(@PathVariable Integer id) {
//        String key = "doctor" + id;
//        Doctor doctor;
//        if(redisUtils.get(key) == null) {
//            doctor = doctorService.selectById(id);
//            redisUtils.set(key, doctor);
//        } else {
//            doctor = (Doctor) redisUtils.get(key);
//        }
//        return Result.success(doctor);
//    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Doctor doctor = doctorService.selectById(id);
        return Result.success(doctor);
    }

//    /**
//     * 查询所有
//     */
//    @GetMapping("/selectAll")
//    public Result selectAll(Doctor doctor) {
//        String key = doctor.getUsername() + doctor.getRole() + "selectAll";
//        List<Doctor> list;
//        if(redisUtils.get(key)==null) {
//             list = doctorService.selectAll(doctor);
//             redisUtils.set(key, list, redisResult.expireTime);;
//        } else {
//            list = (List<Doctor>) redisUtils.get(key);
//        }
//        return Result.success(list);
//    }

    @GetMapping("/selectAll")
    public Result selectAll(Doctor doctor) {
        List<Doctor> doctors = doctorService.selectAll(doctor);
        return Result.success(doctors);
    }

//    /**
//     * 分页查询
//     */
//    @GetMapping("/selectPage")
//    public Result selectPage(Doctor doctor,
//                             @RequestParam(defaultValue = "1") Integer pageNum,
//                             @RequestParam(defaultValue = "10") Integer pageSize) {
//        PageInfo<Doctor> page;
//        String key = doctor.getUsername() + doctor.getRole() + "selectPage";
//        if(redisUtils.get(key)==null) {
//            page = doctorService.selectPage(doctor, pageNum, pageSize);
//            redisUtils.set(key, page, redisResult.expireTime);;
//        } else {
//            page = (PageInfo<Doctor>) redisUtils.get(key);
//        }
//        return Result.success(page);
//    }

    @GetMapping("/selectPage")
    public Result selectPage(Doctor doctor,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue =  "10") Integer pageSize) {
        PageInfo<Doctor> pageInfo = doctorService.selectPage(doctor, pageNum, pageSize);
        return Result.success(pageInfo);
    }


    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        doctorService.deleteBatch(ids);
        return Result.success();
    }
}
